package com.lanyun.aicorrectassistant.controller.admin.studentAssignments;

import com.alibaba.dashscope.exception.ApiException;
import com.google.common.base.Preconditions;
import com.lanyun.aicorrectassistant.controller.admin.studentAssignments.vo.StudentAssignmentsRequestPageVO;
import com.lanyun.aicorrectassistant.controller.admin.studentAssignments.vo.StudentAssignmentsRequestVO;
import com.lanyun.aicorrectassistant.controller.admin.studentAssignments.vo.StudentAssignmentsResponseVO;
import com.lanyun.aicorrectassistant.domain.studentAssignments.StudentAssignmentsDomainService;
import com.lanyun.aicorrectassistant.basic.bean.PageResult;
import com.lanyun.aicorrectassistant.basic.bean.Result;
import com.lanyun.aicorrectassistant.handler.AssignmentGradingService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import reactor.core.publisher.Flux;

import java.nio.charset.StandardCharsets;


/**
 * 学生作业表控制器
 *
 * @author makejava
 * @version 1.0
 * @date 2025-05-12 01:54:21
 */
@RequestMapping("/api-admin/studentAssignments")
@RestController("adminStudentAssignmentsController")
@Slf4j
@CrossOrigin
public class StudentAssignmentsController {

    @Resource
    private StudentAssignmentsDomainService studentAssignmentsDomainService;

    @Resource
    private AssignmentGradingService assignmentGradingService;

    /**
     * 分页查询学生作业
     */
    @PostMapping("/page")
    public Result<PageResult<StudentAssignmentsResponseVO>> page(@RequestBody StudentAssignmentsRequestPageVO requestPageVO) {
        return Result.success(studentAssignmentsDomainService.page(requestPageVO));
    }

    /**
     * 新增学生作业
     */
    @PostMapping("/create")
    public Result create(@RequestBody StudentAssignmentsRequestVO requestVO) {
        try {
            studentAssignmentsDomainService.insert(requestVO);
            return Result.success();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 修改学生作业
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody StudentAssignmentsRequestVO requestVO) {
        return Result.success(studentAssignmentsDomainService.update(requestVO));
    }

    /**
     * 删除学生作业
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(studentAssignmentsDomainService.delete(id));
    }

    /**
     * 获取学生作业详情
     */
    @GetMapping("/get/{id}")
    public Result<StudentAssignmentsResponseVO> get(@PathVariable("id") Long id) {
        return Result.success(studentAssignmentsDomainService.get(id));
    }


    /**
     * 批改作业
     */
    @PostMapping(value = "/grade/{id}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<byte[]> grade(@PathVariable("id") Long id) {
        try {

            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(id, "作业ID不能为空");
            }

            return assignmentGradingService.saveGradingResult(id)
                    .map(s -> s.getBytes(StandardCharsets.UTF_8));
        } catch (IllegalArgumentException iae) {
            return Flux.error(iae);
        } catch (ApiException e) {
            return Flux.error(e);
        }
    }

    /**
     * 提交批改
     */
    @PostMapping("/submitGrading")
    public Result<Boolean> submitGrading(@RequestBody StudentAssignmentsRequestVO requestVO) {
        try {
            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(requestVO, "请求体不能为空");
                Preconditions.checkNotNull(requestVO.getId(), "作业ID不能为空");
                Preconditions.checkNotNull(requestVO.getGradingResult(), "批改结果不能为空");
            }
            return Result.success(studentAssignmentsDomainService.submitGrading(requestVO.getId(), requestVO.getGradingResult()));
        } catch (IllegalArgumentException iae) {
            return Result.fail(iae.getMessage());
        }
    }
}
